package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.service.BBrandInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BPlatformCommodityInfoEntity;
import io.lkt.modules.sys.service.BPlatformCommodityInfoService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 平台商品信息表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2019-04-16 10:09:43
 */
@RestController
@RequestMapping("/sys/bplatformcommodityinfo")
public class BPlatformCommodityInfoController {
    @Autowired
    private BPlatformCommodityInfoService bPlatformCommodityInfoService;

    @Autowired
    private BBrandInfoService bBrandInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:bplatformcommodityinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bPlatformCommodityInfoService.queryPage(params);
        List<BPlatformCommodityInfoEntity> commodity= (List<BPlatformCommodityInfoEntity>) page.getList();
        for(BPlatformCommodityInfoEntity bPlatformCommodityInfoEntity:commodity){
            if(bPlatformCommodityInfoEntity.getBrandId()!=null) {
                bPlatformCommodityInfoEntity.setBrandName(bBrandInfoService.getById(bPlatformCommodityInfoEntity.getBrandId()).getBrandName());
            }
        }
        page.setList(commodity);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:bplatformcommodityinfo:info")
    public R info(@PathVariable("id") Integer id){

        BPlatformCommodityInfoEntity bPlatformCommodityInfo = bPlatformCommodityInfoService.getById(id);
        String brandName=bBrandInfoService.getById(bPlatformCommodityInfo.getBrandId()).getBrandName();
        return R.ok().put("bPlatformCommodityInfo", bPlatformCommodityInfo).put("brandName",brandName);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:bplatformcommodityinfo:save")
    public R save(@RequestBody BPlatformCommodityInfoEntity bPlatformCommodityInfo){
        Integer brandId=bBrandInfoService.queryByName(bPlatformCommodityInfo.getBrandName()).getId();
        bPlatformCommodityInfo.setBrandId(brandId);
        bPlatformCommodityInfoService.save(bPlatformCommodityInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:bplatformcommodityinfo:update")
    public R update(@RequestBody BPlatformCommodityInfoEntity bPlatformCommodityInfo){
        ValidatorUtils.validateEntity(bPlatformCommodityInfo);
        Integer brandId=bBrandInfoService.queryByName(bPlatformCommodityInfo.getBrandName()).getId();
        bPlatformCommodityInfo.setBrandId(brandId);
        bPlatformCommodityInfoService.updateById(bPlatformCommodityInfo);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:bplatformcommodityinfo:delete")
    public R delete(@RequestBody Integer[] ids){
        bPlatformCommodityInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
